from fastapi import APIRouter

from app.database.postgre import SessionDep
from app.models.common import FirewallRuleIdJSON, AppIdJSON
from app.models.firewall_rules import (AppFirewallRuleListParams, CreateFirewallRuleJSON, EnableFirewallRuleJSON,
                                       BatchDeleteAppRulesJSON)
from app.services import accounts, firewall_rules

router = APIRouter(
    prefix='/api/v1/firewall_rules',
    tags=['backend', 'firewall_rules']
)


@router.get('/list')
async def firewall_rules_list(
        account: accounts.LoggedInAccount,
        query_params: AppFirewallRuleListParams,
        session: SessionDep):
    response = await firewall_rules.get_firewall_rules_list(account, query_params, session)
    return response


@router.post('/create')
async def create_firewall_rule(
        account: accounts.LoggedInAccount,
        json_data: CreateFirewallRuleJSON,
        session: SessionDep):
    response = await firewall_rules.create_firewall_rule(account, json_data, session)
    return response


@router.post('/delete')
async def delete_firewall_rule(
        account: accounts.LoggedInAccount,
        json_data: FirewallRuleIdJSON,
        session: SessionDep):
    response = await firewall_rules.delete_firewall_rule(account, json_data, session)
    return response


@router.post('/batch_delete')
async def batch_delete_rules(account: accounts.LoggedInAccount, json_data: BatchDeleteAppRulesJSON,
                             session: SessionDep):
    response = await firewall_rules.delete_firewall_rules(account, json_data, session)
    return response


@router.post('/clear')
async def clear_app_rules(account: accounts.LoggedInAccount, json_data: AppIdJSON, session: SessionDep):
    response = await firewall_rules.clear_all_firewall_rules(account, json_data, session)
    return response


@router.post('/enable')
async def enable_firewall_rule(
        account: accounts.LoggedInAccount,
        json_data: EnableFirewallRuleJSON,
        session: SessionDep):
    response = await firewall_rules.enable_firewall_rule(account, json_data, session)
    return response
